#include<bits/stdc++.h>
#define int long long
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
using namespace std;
int rd() {int x=0,f=1;char c=getchar();while(!isdigit(c))f=c=='-'?-1:f,c=getchar();while(isdigit(c))x=x*10+(c^48),c=getchar();return x*f;}
const int N=1000005;
int n,a[N],c[N],dp[N],mx[N];
void Main() {
    n=rd();
    rep(i,1,n) a[i]=rd(),c[i]=c[i-1]+(a[i]==a[i-1]?a[i]:0);
    memset(mx,-63,sizeof(mx));
    int all=-1e9,ans=-1e9;
    rep(i,1,n) {
        dp[i]=c[i];
        dp[i]=max(dp[i],all+c[i-1]);
        dp[i]=max(dp[i],mx[a[i]]+c[i-1]+a[i]);
        all=max(all,dp[i]-c[i]);
        mx[a[i-1]]=max(mx[a[i-1]],dp[i]-c[i]);
        ans=max(ans,dp[i]+c[n]-c[i]);
    }
    printf("%lld\n",max(c[n],ans));
}
signed main() {
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int T=rd();
    while(T--) Main();
    return 0;
}
//g++ -std=c++14 -O2 -Wall -fsanitize=address,undefined -o color color.cpp